﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ProxyServer.EntityFrameworkCore;
using ProxyServer.EntityFrameworkCore.Sqlite;

namespace ProxyServer;

public class SqliteDesignTimeFactory : AbstractDesignTimeFactory
{
    public SqliteDesignTimeFactory() : base()
    {
    }

    public SqliteDesignTimeFactory(string[] args) : base(args)
    {
    }

    public SqliteDesignTimeFactory(IConfiguration configuration) : base(configuration)
    {
    }

    public override AppDbContext CreateDbContext(string[] args)
    {
        var connectionString = BuildConfiguration(args).GetConnectionString(DatabaseProviderNames.Sqlite);

        ArgumentNullException.ThrowIfNullOrWhiteSpace(connectionString, nameof(connectionString));
        Console.WriteLine("{0}: {1}", DatabaseProviderNames.Sqlite, connectionString);

        var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
        optionsBuilder.UseSqlite(connectionString, sqlite => sqlite.MigrationsAssembly("ProxyServer.Sqlite"));
        
        return new AppDbContext(optionsBuilder.Options, new ModelBuilderOptions<AppDbContext>(SqliteModelBuilder.ConfigureModel));
    }
}
